package beliberda;

import java.io.IOException;
import java.util.List;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import account.Account;
import account.AccountDAO;

import com.google.appengine.api.blobstore.BlobstoreService;
import com.google.appengine.api.blobstore.BlobstoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.users.User;
import com.googlecode.objectify.NotFoundException;

@SuppressWarnings("serial")
public class HistoryServlet extends AbstractHistoryServlet {

	private static final Logger log = Logger.getLogger(HistoryServlet.class.getName());

	private HistoryDAO dao = new HistoryDAO();
	@Override
	protected void showForm(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
	    resp.sendRedirect("/blobstore");
	    log.info("from showForm - OK");
	}

	@Override
	protected void handleSubmit(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
	}

	@Override
	protected void showHistories(String nick, HttpServletRequest req,
			HttpServletResponse resp) throws ServletException, IOException {
		List<History> lh = dao.getHistories();
		resp.setContentType("text/html");
        resp.getWriter().println("Hello, " + nick+"!<br/>");
        resp.getWriter().println("<a href='/history?action=create&nick='"+nick+">Create new history</a><br/>");
        resp.getWriter().println("<br/><center>HistoriesList:<br/>");
        resp.getWriter().println("<table><tr><td>CreationDate</td><td>Author</td><td>Title</td><td>Genre</td><td>MaxLenght</td><td>Total</td></tr>");
        for(History h : lh){
        	resp.getWriter().println("<tr><td>"+h.getDate().toString()+"</td>"+
        			                     "<td>"+h.getAuthor().getNick()+"</td>" +
        			                     "<td><a href='/history?action=read&id='"+h.getId()+">"+h.getTitle()+"</a></td>" +
        			                     "<td>"+h.getGenre()+"</td>" +
        			                     "<td>Unknown</td>"+
        			                     "<td>Unknown</td>"+
        					"</tr>");
        }
        resp.getWriter().println("</table></center><br/>");
	}

	@Override
	protected void showHistory(long id, HttpServletRequest req,
			HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void createHistory(String nick, HttpServletRequest req,
			HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		req.setAttribute("nick", nick);
	    req.getRequestDispatcher("createhistory.jsp").forward(req, resp);
	}
	
}
